package com.molyfun.parents.activity.myorder;

import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.myorder.view.OrderView;
import com.molyfun.parents.rest.bean.OrderDetailResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:9/19/16.
 */
public class OrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private List<OrderDetailResponse.OrderBean> mRecordList;
    private OrderView.OrderViewCallback mOrderViewCallback;
    private FragmentManager mFragmentManager;

    public OrderAdapter(Context mContext, OrderView.OrderViewCallback orderViewCallback, FragmentManager fragmentManager) {
        this.mContext = mContext;
        this.mOrderViewCallback = orderViewCallback;
        this.mFragmentManager = fragmentManager;
    }

    //加载更多，放到队列最后
    public void addAll(List<OrderDetailResponse.OrderBean> list){
        if(mRecordList == null){
            mRecordList = new ArrayList<>();
        }
        mRecordList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearAll(){
        if(mRecordList != null){
            mRecordList.clear();
            notifyDataSetChanged();
        }
    }

    public void refreshData(List<OrderDetailResponse.OrderBean> list){
        this.mRecordList = list;
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OrderViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_order_cell,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        OrderDetailResponse.OrderBean orderBean = mRecordList.get(position);
        OrderViewHolder orderViewHolder = (OrderViewHolder) holder;
        orderViewHolder.mOrderView.initData(orderBean, mFragmentManager, (position != getItemCount() - 1));
        orderViewHolder.mOrderView.setOrderViewCallback(mOrderViewCallback);
        orderViewHolder.mOrderView.setOnOrderOutOfDateCallback(new OnOrderOutOfDateCallback() {
            @Override
            public void onOrderOutOfDate(OrderDetailResponse.OrderBean orderBean) {
                mRecordList.remove(orderBean);
//                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        if(mRecordList == null){
            return 0;
        }else{
            return mRecordList.size();
        }
    }

    class OrderViewHolder extends RecyclerView.ViewHolder{

        @InjectView(R.id.order_layout)
        OrderView mOrderView;

        public OrderViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }

    }

    public interface OnOrderOutOfDateCallback{
        public void onOrderOutOfDate(OrderDetailResponse.OrderBean orderBean);
    }

}
